home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / Source / Chapter 10 / Engine / RenderCache.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2005-03-29  |  3.9 KB  |  110 lines

  1. //-----------------------------------------------------------------------------
  2. // RenderCache.h implementation.
  3. // Refer to the RenderCache.h interface for more details.
  4. //
  5. // Programming a Multiplayer First Person Shooter in DirectX
  6. // Copyright (c) 2004 Vaughan Young
  7. //-----------------------------------------------------------------------------
  8. #include "Engine.h"
  9.  
  10. //-----------------------------------------------------------------------------
  11. // The render cache class constructor.
  12. //-----------------------------------------------------------------------------
  13. RenderCache::RenderCache( IDirect3DDevice9 *device, Material *material )
  14. {
  15.     m_device = device;
  16.  
  17.     m_material = material;
  18.  
  19.     m_indexBuffer = NULL;
  20.     m_totalIndices = 0;
  21. }
  22.  
  23. //-----------------------------------------------------------------------------
  24. // The render cache class destructor.
  25. //-----------------------------------------------------------------------------
  26. RenderCache::~RenderCache()
  27. {
  28.     SAFE_RELEASE( m_indexBuffer );
  29. }
  30.  
  31. //-----------------------------------------------------------------------------
  32. // Increases the size of the render cache to manage another face.
  33. //-----------------------------------------------------------------------------
  34. void RenderCache::AddFace()
  35. {
  36.     m_totalIndices += 3;
  37. }
  38.  
  39. //-----------------------------------------------------------------------------
  40. // Prepares the render cache for use. Must be called after setting the size.
  41. //-----------------------------------------------------------------------------
  42. void RenderCache::Prepare( unsigned long totalVertices )
  43. {
  44.     // Set the total vertices that are rendered.
  45.     m_totalVertices = totalVertices;
  46.  
  47.     // Create the render cache's index buffer.
  48.     m_device->CreateIndexBuffer( m_totalIndices * sizeof( unsigned short ), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &m_indexBuffer, NULL );
  49. }
  50.  
  51. //-----------------------------------------------------------------------------
  52. // Informs the render cache that rendering is about to begin.
  53. //-----------------------------------------------------------------------------
  54. void RenderCache::Begin()
  55. {
  56.     m_indexBuffer->Lock( 0, 0, (void**)&m_indexPointer, 0 );
  57.  
  58.     m_faces = 0;
  59. }
  60.  
  61. //-----------------------------------------------------------------------------
  62. // Adds the given face indicies to be rendered.
  63. //-----------------------------------------------------------------------------
  64. void RenderCache::RenderFace( unsigned short vertex0, unsigned short vertex1, unsigned short vertex2 )
  65. {
  66.     *m_indexPointer++ = vertex0;
  67.     *m_indexPointer++ = vertex1;
  68.     *m_indexPointer++ = vertex2;
  69.  
  70.     m_faces++;
  71. }
  72.  
  73. //-----------------------------------------------------------------------------
  74. // Informs the render cache that rendering is finished, so render the faces.
  75. //-----------------------------------------------------------------------------
  76. void RenderCache::End()
  77. {
  78.     // Unlock the index buffer.
  79.     m_indexBuffer->Unlock();
  80.  
  81.     // Check if there are any faces to render.
  82.     if( m_faces == 0 )
  83.         return;
  84.  
  85.     // Check if the material should ignore fog.
  86.     if( m_material->GetIgnoreFog() == true )
  87.         m_device->SetRenderState( D3DRS_FOGENABLE, false );
  88.  
  89.     // Set the material and texture.
  90.     m_device->SetMaterial( m_material->GetLighting() );
  91.     m_device->SetTexture( 0, m_material->GetTexture() );
  92.  
  93.     // Set the indicies to render the correct faces.
  94.     m_device->SetIndices( m_indexBuffer );
  95.  
  96.     // Render all the faces.
  97.     m_device->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 0, m_totalVertices, 0, m_faces );
  98.  
  99.     // Restore the fog setting if it was changed.
  100.     if( m_material->GetIgnoreFog() == true )
  101.         m_device->SetRenderState( D3DRS_FOGENABLE, true );
  102. }
  103.  
  104. //-----------------------------------------------------------------------------
  105. // Returns a pointer to the material being used by this render cache.
  106. //-----------------------------------------------------------------------------
  107. Material *RenderCache::GetMaterial()
  108. {
  109.     return m_material;
  110. }